<?php

class CF_FilterFormFull {
	/**
	* @var string $selfpage References the page (usually $_PHP['SELF'])
	*/
	var $selfpage;
	var $cs_name;
	/**
	* @var string $feildspec describes columns and option
	*/	
	var $fieldspec;
	/**
	* @var string $filterprefix prefix for filtered variables (column name used)
	*/
	var $filterprefix = 'flt_';	
	/**
	* @var array $filters Holds fitlers as array filers[<column>] = <value> filled by get_filter_vals().  This expects filter variables form the $_REQUEST or from the $_SESSION if saved (pass empty to clear).  Uses prefix determined by $filterprefix
	*/
	var $filters; 
	/**
	* @var bool $submit_on_change Determines whether to submit filter form when anything changes (no longer implemetned)
	*/
	var $submit_on_change = false;
	/**
	* @var bool $show_filter_submit show sumbit filter (Default true)
	*/   	
    var $show_filter_submit = true;
    
	/**
	* @var string $show_filter_value string to show on filter button (default 'Apply')
	*/   	
	var $show_filter_value = 'Apply';
	/**
	* @var string $addmarkup string of markup to put in add form for other hidden fields, etc.
	*/  	
	
	function __construct($fieldspec,$filters,$selfpage,$cs_name='defcs'){
		$this->fieldspec = $fieldspec;
		$this->filters = $filters;
		$this->cs_name = $cs_name;
		if($selfpage == ''){
			$selfpage = $_SERVER['PHP_SELF'];
		}
		$this->selfpage = $selfpage;
	}
	
	/**
	 *
	 * make_filters_box  Method to create a fitlers box which will show each editable
	 * value in it's own text entry or drop-down box for filtering
	 * @param string $method Form Method (get or post)
	 *
	 */
    function make_filters_box($method = 'get'){
        $none_entry[1]['keyvalue'] = '';
		$none_entry[1]['showvalue'] = '';
		$showbox = false;
		$filterbox .= "<table class='filterbox'><form name='filters_box' action='$this->selfpage' method='$method'>\n";
        $count = 1;
        foreach($this->fieldspec AS $col_name => $colspec){
		$changefunction = '';
		if($this->submit_on_change){
			$changefunction = 'this.form.submit()';
		}        
			if((!empty($colspec['filter']) ) AND (!empty($colspec['showfilter']))){ // insert AND 
				// OK, next, check if this is an array
				if(is_array($colspec['filter'])){
					$showbox = true;

					foreach($colspec['filter'] AS $opkey => $filterspec){
						// set heading name
						if(!empty($filterspec['filterhead'])){
							$elements[$count]['head'] = $filterspec['filterhead'];
						}
						else{
							//$elements[$count]['head'] = $filterspec['filtervar'];
							$elements[$count]['head'] = $opkey;
							//print_r($colspec['filter']);
						}
						$filtername = $opkey;
						if(is_array($filterspec['filterfield'])){
							$elements[$count]['body'] = MakeEditField($filterspec['filterfield'], $filtername, $this->filters[$filtername]['val'], $changefunction, $filtername, $none_entry,$this->top_dir);
						}
						else{
							$elements[$count]['body'] = MakeEditField($this->fieldspec[$col_name], $filtername, $this->filters[$filtername]['val'], $changefunction, $filtername, $none_entry,$this->top_dir);							
						}
						$count ++;
					} // end each filterspec
					
				} // End is array 
				else{
				
					$showbox = true;
					// heading name
					if(!empty($colspec['headname'])){
						$elements[$count]['head'] = $colspec['headname'] ;
					}else{
						$elements[$count]['head'] =  $col_name;
					}
					$filtername = $this->filterprefix . $col_name;
					$elements[$count]['body'] = MakeEditField($this->fieldspec[$col_name], $filtername, $this->filters[$filtername]['val'], $changefunction, $filtername, $none_entry,$this->top_dir);
				} // end filterspec is constant
				$count ++;
			}// end colpsec filter set
        }// end foreach field
        $filterbox .= $this->make_element_set($elements, "class='filter_head'","class='filter_head'");
        if($this->show_filter_submit){
            $filterbox .= "<tr><td colspan='$count' > <input type='hidden' name='$this->startatvar' value='0'> <input type='submit' id='refresh' name='refresh' value='$this->show_filter_value'></form> </td></tr>\n";
        }else{
            $filterbox .= "</form>";
         }
         $filterbox .= "</table>";
	if($showbox){	
		return $filterbox;
	}
	else{
		return '';
	}
    }
	/**
	 *
     * @make_element set will create a line of headed elements arranged in a table
     * from an input of an element_set array.  Each row has element_name and value
     * @param array elements An array with [body] and [head] parts where head
     * elements['head'] is the title and elements['body'] is what to put in the cell
     * @param string $headset Settings for heading elements (class name, etc)
     * @param string $bodyset Settings for body elemetns (class naem, etc)
	 *
	 */
    function make_element_set($elements, $headset, $bodyset){

        if(is_array($elements)){
        foreach ($elements AS $current){
            $headtext .= "<th>" . $current['head'] . "</th>\n";
            $bodytext .=  "<td>" . $current['body'] . "</td>\n";
        }
        }
        return "<tr $headset>$headtext </tr> <tr $bodyset>$bodytext</tr>";
    }


}
